﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Website.Models;

namespace Website.Controllers
{ 
    public class CategoryController : Controller
    {
        private KFashionEntities db = new KFashionEntities();

        //
        // GET: /Category/

        public ViewResult Index()
        {
            var categories = db.Categories.Where(c => !c.ParentCategoryID.HasValue).OrderByDescending(c=>c.CategoryID);
            return View(categories.ToList());
        }

        //
        // GET: /Category/Details/5

        public ViewResult Details(int id)
        {
            Category category = db.Categories.Single(c => c.CategoryID == id);
            return View(category);
        }

        public ActionResult CreateSubCategory(int id)
        {
            var parent = db.Categories.First(c => c.CategoryID == id);
            var sub = new Category
                          {
                              Gender = parent.Gender,
                              ParentCategory = parent,
                              ParentCategoryID = parent.CategoryID,
                              Display = true
                          };
            return View(sub);
        }

        [HttpPost]
        public ActionResult CreateSubCategory(Category category)
        {
            if (ModelState.IsValid)
            {
                db.Categories.AddObject(category);
                db.SaveChanges();
                return RedirectToAction("Details", new {id = category.ParentCategoryID});
            }
            
            return View(category);
        }

        //
        // GET: /Category/Create

        public ActionResult Create()
        {
            //ViewBag.ParentCategoryID = new SelectList(db.Categories, "CategoryID", "CategoryName");
            return View();
        } 

        //
        // POST: /Category/Create

        [HttpPost]
        public ActionResult Create(Category category)
        {
            if (ModelState.IsValid)
            {
                db.Categories.AddObject(category);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.ParentCategoryID = new SelectList(db.Categories, "CategoryID", "CategoryName", category.ParentCategoryID);
            return View(category);
        }
        
        //
        // GET: /Category/Edit/5
 
        public ActionResult Edit(int id)
        {
            Category category = db.Categories.Single(c => c.CategoryID == id);
            ViewBag.ParentCategoryID = new SelectList(db.Categories, "CategoryID", "CategoryName", category.ParentCategoryID);
            return View(category);
        }

        //
        // POST: /Category/Edit/5

        [HttpPost]
        public ActionResult Edit(Category category)
        {
            if (ModelState.IsValid)
            {
                db.Categories.Attach(category);
                db.ObjectStateManager.ChangeObjectState(category, EntityState.Modified);
                db.SaveChanges();
                if (category.ParentCategoryID.HasValue)
                    return RedirectToAction("Details", new {id = category.ParentCategoryID});
                return RedirectToAction("Index");
            }
            ViewBag.ParentCategoryID = new SelectList(db.Categories, "CategoryID", "CategoryName", category.ParentCategoryID);
            return View(category);
        }

        //
        // GET: /Category/Delete/5
 
        public ActionResult Delete(int id)
        {
            Category category = db.Categories.Single(c => c.CategoryID == id);
            return View(category);
        }

        //
        // POST: /Category/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Category category = db.Categories.Single(c => c.CategoryID == id);
            db.Categories.DeleteObject(category);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult GetCategoryByGender(bool gender)
        {
            var categories = db.Categories.Where(c=>!c.ParentCategoryID.HasValue).Include("SubCategories").Where(c => c.Gender == gender);
            ViewBag.CategoryGender = gender;
            return PartialView(categories);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}